package login

import (
	session2 "OnlineRetailers/controllers/admin/session"
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/models/Admin/resMes"
	error2 "OnlineRetailers/models/errorDeal"
	"fmt"
	"github.com/astaxie/beego"
)

type GetSession struct {
	beego.Controller
}

func (c *GetSession) GetSession() {

	log := &error2.Log{}
	mes := &resMes.Session{}

	var err error
	var session string

	defer func() {

		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.Ctx.ResponseWriter.Header().Set("Access-Control-Expose-Headers", "Session")
		c.Ctx.ResponseWriter.Header().Set("Session", session)
		c.ServeJSON()

	}()
	var addr = "/login/GetSession"

	sessionUnkown := c.Ctx.Request.Header["Session"]

	if len(sessionUnkown) != 0 {
		session = sessionUnkown[0]
		fmt.Println(session)
		err = session2.DestroySession(session)
		if err != nil {
			c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
			return
		}
	}

	if session, err = session2.GenerationSession(); err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	mes.CaptchaId = initCaptcha()
	mes.Mes = "success"
	mes.Code = "200"
	c.Data["json"] = mes

}
